package pt.inescid.components.policyengine.heimdall.pdp;

import java.io.Serializable;
import java.util.Vector;

public class PolicyDecision implements Serializable{

	private int value;
	private int status;
	
	public static final String[] DECISION_VALUE_STRINGS = {
		"AUTHORIZE",
		"DENY",
		"N/A",
		"TIMEOUT",
		"UNDEFINED",
		"RELEVANT",
		"IRRELEVANT"
	};
	
	public static final String[] DECISION_STATUS_STRINGS = {
		"UNDEFINED",
		"WAIT_ACTIV",
		"WAIT_INFO",
		"FINAL",
		"WAIT_OBLIG",
		"WAIT_OBLIG_INFO",
		"FINAL_OBLIG",
	};
	
	public static final int DECISION_AUTHORIZE = 0;
	public static final int DECISION_DENY = 1;
	public static final int DECISION_NOT_APPLICABLE = 2;
	public static final int DECISION_TIMEOUT = 3;
	public static final int DECISION_UNDEFINED = 4;
	public static final int DECISION_INFLUENCED_BY_LEASE = 4;
	
	public static final int EVALUATION_RELEVANT = 5;
	public static final int EVALUATION_IRRELEVANT = 6;

	public static final int STATUS_UNDEFINED = 0;
	public static final int STATUS_WAITING_ACTIVATION = 1;
	public static final int STATUS_WAITING_INFO = 2;
	public static final int STATUS_FINAL = 3;
	public static final int STATUS_WAIT_OBLIG = 4;
	public static final int STATUS_WAIT_OBLIG_INFO = 5;
	public static final int STATUS_FINAL_OBLIG = 6;

	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      Constructor       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public PolicyDecision () {
		this(DECISION_NOT_APPLICABLE, STATUS_UNDEFINED);
	}

	public PolicyDecision (int paramValue, int paramState) {
		value = paramValue;
		status = paramState;
	}
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      getDecision       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public int getValue() {
		return (value);
	}

	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      and       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static PolicyDecision and(Vector allDecisions) {
		int thisDecisionValue = DECISION_UNDEFINED;
		int thisDecisionState = STATUS_UNDEFINED;
		PolicyDecision currentDecision = null;
		
		for (int i = 0; i < allDecisions.size(); i++) {
			currentDecision = (PolicyDecision) allDecisions.elementAt(i);
			
			// decision value
			if (currentDecision.getValue() == DECISION_DENY) {
				thisDecisionValue = DECISION_DENY;
				i = allDecisions.size();
			}
			else if (currentDecision.getValue() == DECISION_AUTHORIZE) {
				thisDecisionValue = DECISION_AUTHORIZE;
			}
			
			// decision state
			if (thisDecisionState == STATUS_UNDEFINED) {
				thisDecisionState = currentDecision.getStatus();
			}
			else if (currentDecision.getStatus() == STATUS_WAITING_INFO) {
				thisDecisionState = STATUS_WAITING_INFO;
			}
		}
		
		return (new PolicyDecision(thisDecisionValue, thisDecisionState));
	}

//  ----------------------------------------------------------
//  ------------------------   toString   ------------------------
//  ----------------------------------------------------------
	public String toString() {
		String result = "";
		
		if (status > DECISION_STATUS_STRINGS.length-1 ||
				value > DECISION_VALUE_STRINGS.length-1) {
			status = 0;
		}
		result += DECISION_STATUS_STRINGS[status] + "/" +
				DECISION_VALUE_STRINGS[value];
		return (result);
	}


}; // PolicyDecision
